package ru.usu.gv.view;

import ru.usu.gv.gui.actions.ActionBase;
import ru.usu.gv.gui.actions.DeleteEdgeAction;
import ru.usu.gv.gui.frames.EditEdgeFrame;
import ru.usu.gv.uicomponents.UIEdge;

import javax.swing.AbstractAction;
import javax.swing.JPopupMenu;

import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;

/**
 @author Sergey Pupyrev 
 11.01.2007
 */

public class PopupEdgePickingClient extends PickingClientBase<UIEdge>
{
	private JPopupMenu edgePopupMenu = null;

	public void callback(MouseEvent e, UIEdge element)
	{
		if (!e.isMetaDown())
			return;

		initilizePopupMenu(element);
		edgePopupMenu.show(e.getComponent(), e.getX(), e.getY());
		edgePopupMenu.updateUI();
	}

	private void initilizePopupMenu(final UIEdge edge)
	{
		edgePopupMenu = new JPopupMenu();
		edgePopupMenu.add(new AbstractAction("Редактировать")
		{
			public void actionPerformed(ActionEvent e)
			{
				new EditEdgeFrame(edge).setVisible(true);
			}
		});

		edgePopupMenu.add(new AbstractAction("Удалить все сгибы")
		{
			public void actionPerformed(ActionEvent e)
			{
				edge.getViewItem().rectificate();
				edge.draw();
			}
		});

		edgePopupMenu.add(new AbstractAction("Удалить")
		{
			public void actionPerformed(ActionEvent e)
			{
				ActionBase action = new DeleteEdgeAction(edge);
				action.execute();
			}
		});
	}
}
